package com.qfedu.controller;

import com.github.pagehelper.PageHelper;
import com.qfedu.entity.Askforleave;
import com.qfedu.entity.ResponseData;
import com.qfedu.service.AskforleaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping
public class AskforleaveController {
    @Autowired
    private AskforleaveService askforleaveService;

    @RequestMapping("/addAskforleave")
    @ResponseBody
    public ResponseData addAskforleave(Askforleave askforleave){
        boolean result = askforleaveService.addAskforleave(askforleave);
        ResponseData responseData = new ResponseData();

        if(result){
            responseData.setCode(ResponseData.SUCCESS_CODE);// 成功
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else{
            responseData.setCode(ResponseData.FAIL_CODE);// 成功
            responseData.setMsg(ResponseData.FAIL_MSG);
        }

        return responseData;
    }

    @RequestMapping("/findAllAskforleave")
    @ResponseBody
    public ResponseData findAllAskforleave(int page,int limit){

       return askforleaveService.findAllAskforleave(page, limit);

    }

    @RequestMapping("/deleteAskforleave")
    @ResponseBody
    public ResponseData deleteAskforleave(int id){
        Boolean s = askforleaveService.deleteAskforleave(id);
        ResponseData responseData = new ResponseData();
        if (s){
            responseData.setCode(ResponseData.SUCCESS_CODE);// 成功
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);// 成功
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;
    }

    @RequestMapping("/updateAskforleave")
    @ResponseBody
    public ResponseData updateAskforleave(Askforleave askforleave){
        boolean b = askforleaveService.updateAskforleave(askforleave);
        ResponseData responseData = new ResponseData();
        if (b){
            responseData.setCode(ResponseData.SUCCESS_CODE);// 成功
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);// 成功
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;

    }


    @RequestMapping("/askUpdate")

    public String getAskForLeaveById(int id, HttpServletRequest request){
        Askforleave ask = askforleaveService.getAskForById(id);
        request.setAttribute("ask",ask);

        return "/AskForLeaveUpdate.jsp";
    }


    @RequestMapping("/updateAsk")

    public ResponseData updateAsk(int id, String name, Date startTime,Date endTime,String reason){
        Askforleave askforleave = new Askforleave();
        askforleave.setId(id);
        askforleave.setName(name);
        askforleave.setStartTime(startTime);
        askforleave.setEndTime(endTime);
        askforleave.setReason(reason);
        askforleaveService.updateAskforleave(askforleave);
        ResponseData responseData = new ResponseData();
        responseData.setData(askforleave);
        responseData.setCode(1);
        return responseData;
    }

}
